package com.gidgabs.modules.sys.controller;

import com.gidgabs.common.utils.PageUtils;
import com.gidgabs.common.utils.Query;
import com.gidgabs.common.utils.R;
import com.gidgabs.modules.entity.TbCompanyEntity;
import com.gidgabs.modules.sys.service.SysCompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 公司信息表
 * 
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-07 18:07:05
 */
@RestController
@RequestMapping("/sys/company")
public class SysCompanyController {
	@Autowired
	private SysCompanyService companyService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("company:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TbCompanyEntity> companyList = companyService.queryList(query);
		int total = companyService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(companyList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbCompanyEntity company = companyService.queryObject(id);
		return R.ok().put("company", company);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("company:save")
	public R save(@RequestBody TbCompanyEntity company){
		if(companyService.queryByObject(new TbCompanyEntity()) != null){
			return R.error("系统当前只允许添加一条公司信息数据！");
		}
		companyService.save(company);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("company:update")
	public R update(@RequestBody TbCompanyEntity company){
		companyService.update(company);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("company:delete")
	public R delete(@RequestBody Integer[] ids){
		companyService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
